
import { config } from './Config';
const { ccclass, property } = cc._decorator;

@ccclass
export default class Home extends cc.Component {

    @property(cc.Prefab)
    alert_prefab: cc.Prefab = null;

    @property(cc.Node)
    node_home: cc.Node = null;

    @property(cc.Node)
    node_help: cc.Node = null;

    @property(cc.Toggle)
    toggle_voice: cc.Toggle = null;

    @property([cc.Toggle])
    levelList: cc.Toggle[] = [];

    @property([cc.SpriteFrame])
    lvImgList: cc.SpriteFrame[] = [];

    onLoad() {

        this.node_home.active = true;
        this.node_help.active = false;

        this.node_help.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            this.node_help.active = false;
        });

        this.levelList.forEach((lv, index) => {
            let bg = cc.find('Background', lv.node);
            if (bg) {
                bg.getComponent(cc.Sprite).spriteFrame = (index + 4) > config.lockLevel ? this.lvImgList[1] : this.lvImgList[0];
            }
        });
        this.levelList[config.curLevel - 3].isChecked = true;
    }

    click_btn_start() {
        cc.director.loadScene('GameScene');
    }

    click_toggle_level(tg_level: cc.Toggle, index) {
        if (Number(index) + 3 >= config.lockLevel) {
            this.levelList[config.curLevel - 3].isChecked = true;
            this.createAlert('还未解锁');
            return;
        }
        config.curLevel = Number(index) + 3;
    }
    click_btn_help() {
        this.node_help.active = true;
    }
    click_btn_voice() {
        cc.log('--点击声音--' + this.toggle_voice.isChecked);
    }
    click_btn_share() {
        this.createAlert('还不能分享哦');
    }
    click_btn_other() {
        this.createAlert('未开放');
    }

    createAlert(msg: string) {
        let alert = cc.instantiate(this.alert_prefab);
        let script = alert.getComponent('Alert');
        script.showAlert(msg);
        alert.parent = this.node;
    }
}
